if (!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.permissions.RemovePermissionsMulti")) {

/**
    **/
    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.userMgt.permissions", function (permissions) {
    });

    /**
    **/
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.userMgt.permissions.RemovePermissionsMulti",
            jsx3.gui.Dialog,
            null,
            function (permissions) {

                /**
                 * This function will be called after deserialize
                 */
                permissions.prototype.onAfterDeserialize = function() {
                    var me = this;
                    this.server = tibco.admin.Main.getServer();
                    this.mtxview = this.getDescendantOfName("mtxView_24_X");
                    this.selectAllBtn = this.getDescendantOfName("btnSelectAll_24_X");
                    this.selectAllBtn.selectAll = function() {
                        me.selectAll();
                    }
                    this.deselectAllBtn = this.getDescendantOfName("btnDeselectAll_24_X");
                    this.deselectAllBtn.deselectAll = function() {
                        me.deselectAll();
                    }

                    // initial UI interface state
                    this.hasPermission = false;
                    this.hasChecked = false;
                    this.getDescendantOfName("btnOK_24_X").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                };

                /**
                 * this function used to initialize matrix list.
                 */
                permissions.prototype.initialize = function(objEvent) {
                    this.entityType = objEvent.entityType;

                    // create a new entitylist cdf object, then get permission data.
                    var cdfListOfEntity = jsx3.xml.CDF.newDocument();
                    this.server.getCache().setDocument("XMLCacheListOfEntity", cdfListOfEntity);
                    for (var i = 0; i < objEvent.entities.length; i++)
                    {
                        var node = cdfListOfEntity.createNode(jsx3.xml.Entity.TYPEELEMENT, "record");
                        node.setAttribute("entityId", objEvent.entities[i].id);
                        node.setAttribute("name", objEvent.entities[i].name);

                        cdfListOfEntity.appendChild(node);
                    }
                    this.callgetBulkPermissions();
                };

                /**
                 * Used for select all checkbox in matrix object
                 */
                permissions.prototype.selectAll = function() {
                    this.onSelectAll(this.mtxview);
                };

                /**
                 * Used for unselect all checkbox in matrix object
                 */
                permissions.prototype.deselectAll = function() {
                    this.onDeselectAll(this.mtxview);
                };

                /**
                 * Make the specified matrix object select all checkboxs
                 */
                permissions.prototype.onSelectAll = function(mtx) {
                    mtxCache = mtx.getXML();
                    var mycollection = mtxCache.selectNodes("//record");
                    while (mycollection.hasNext()) {
                        mycollection.next().setAttribute("checked", "1");
                    }
                    mtx.repaint();
                    this.mtxView_OnToggle();
                };

                /**
                 * Make the specified matrix object unselect all checkboxs
                 */
                permissions.prototype.onDeselectAll = function(mtx) {
                    mtxCache = mtx.getXML();
                    var mycollection = mtxCache.selectNodes("//record");
                    while (mycollection.hasNext()) {
                        mycollection.next().setAttribute("checked", "0");
                    }
                    mtx.repaint();
                    this.mtxView_OnToggle();
                };


                /**
                 * Get bulk permissions form server
                 */
                permissions.prototype.callgetBulkPermissions = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/userMgt/window/permissions/rules/getBulkPermissions.xml",
                            "getBulkPermissions",
                            "Get Bulk Permissions");

                    objService.entityType = this.entityType;
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getPermissionsEndpointURL());

                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetBulkPermissionsSuccess);
                    // objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.ongetBulkPermissionsError);
                    // objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.ongetBulkPermissionsInvalid);
                    objService.doCall();
                };

                permissions.prototype.ongetBulkPermissionsSuccess = function(objEvent) {
                    var cdfBulkPermissions = this.server.getCache().getDocument("XMLCacheGetBulkPermissions");
                    this.mtxview.setSourceXML(cdfBulkPermissions);
                    this.mtxview.repaint();
                };

                permissions.prototype.ongetBulkPermissionsError = function(objEvent) {
                };

                permissions.prototype.ongetBulkPermissionsInvalid = function(objEvent) {
                };

                /**
                 * Called when user click "OK" button
                 */
                permissions.prototype.btnOK_OnClick = function() {
                    this.createSetBulkPermissionsCDF();
                    this.callsetBulkPermissions();
                };

                /**
                 * Create CDFs for request, the request mapping rule will combine them automaticly.
                 * After call this function
                 * "XMLCacheUserPermissions", "XMLCacheGroupPermissions" CDFs will generate.
                 */
                permissions.prototype.createSetBulkPermissionsCDF = function() {
                    var hasView, hasEdit, hasOwner;
                    if (this.getDescendantOfName("radRemoveAll_24_X").getSelected() == jsx3.gui.RadioButton.SELECTED)
                    {
                        hasView = false;
                        hasEdit = false;
                        hasOwner = false;
                    }
                    else
                    {
                        hasView = this.getDescendantOfName("radRemoveView_24_X").getSelected() == jsx3.gui.RadioButton.UNSELECTED;
                        hasEdit = this.getDescendantOfName("radRemoveEdit_24_X").getSelected() == jsx3.gui.RadioButton.UNSELECTED;
                        hasOwner = this.getDescendantOfName("radRemoveOwner_24_X").getSelected() == jsx3.gui.RadioButton.UNSELECTED;
                    }

                    // create and bind two CDFs to "XMLCacheUserPermissions", "XMLCacheGroupPermissions"
                    var cdfUserPermissions = jsx3.xml.CDF.newDocument();
                    var cdfGroupPermissions = jsx3.xml.CDF.newDocument();
                    this.server.getCache().setDocument("XMLCacheUserPermissions", cdfUserPermissions);
                    this.server.getCache().setDocument("XMLCacheGroupPermissions", cdfGroupPermissions);

                    var selectedList = this.mtxview.getXML().selectNodes("//record[@checked='1']");
                    var listLength = selectedList.size();

                    for (var i = 0; i < listLength; i++)
                    {
                        var node = cdfUserPermissions.createNode(jsx3.xml.Entity.TYPEELEMENT, "record");
                        node.setAttribute("hasView", hasView);
                        node.setAttribute("hasEdit", hasEdit);
                        node.setAttribute("hasOwner", hasOwner);

                        if (selectedList.get(i).getAttribute("userId"))
                        {
                            node.setAttribute("userId", selectedList.get(i).getAttribute("userId"));
                            cdfUserPermissions.appendChild(node);
                        }
                        else
                        {
                            node.setAttribute("groupId", selectedList.get(i).getAttribute("groupId"));
                            cdfGroupPermissions.appendChild(node);
                        }
                    }
                };

                /**
                 * Make "OK" button enabled or disabled.
                 */
                permissions.prototype.setBtnOKEnabled = function(enabled) {
                    this.getDescendantOfName("btnOK_24_X").setEnabled(
                            enabled ? jsx3.gui.Form.STATEENABLED : jsx3.gui.Form.STATEDISABLED, true);
                };

                /**
                 * call this when user click any remove buttons.
                 */
                permissions.prototype.radRemovePermission_OnSelect = function() {
                    if (this.hasPermission) return;

                    this.hasPermission = true;
                    this.setBtnOKEnabled(this.hasChecked && this.hasPermission);
                };

                /**
                 * called when user click checkbox in front of matrix items
                 */
                permissions.prototype.mtxView_OnToggle = function() {
                    var selectedList = this.mtxview.getXML().selectNodes("//record[@checked='1']");
                    this.hasChecked = selectedList.size() > 0 ? true : false;
                    this.setBtnOKEnabled(this.hasChecked && this.hasPermission);
                };

                /**
                 * Get bulk permissions form server backend.
                 */
                permissions.prototype.callsetBulkPermissions = function() {

                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/rules/setBulkPermissions.xml", "setBulkPermissions", "Set Bulk Permissions");

                    objService.entityType = this.entityType;
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getPermissionsEndpointURL());

                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onsetBulkPermissionsSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onsetBulkPermissionsError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onsetBulkPermissionsInvalid);
                    objService.doCall();
                };

                permissions.prototype.onsetBulkPermissionsSuccess = function(objEvent) {
                    this.doClose();
                };

                permissions.prototype.onsetBulkPermissionsError = function(objEvent) {
                    this.doClose();
                };

                permissions.prototype.onsetBulkPermissionsInvalid = function(objEvent) {
                    this.doClose();
                };

            });
}
